package com.iwhalecloud.bss.kite.cucc.common.util;

import com.iwhalecloud.bss.kite.client.dto.inst.InstAttrDTO;
import com.iwhalecloud.bss.kite.client.vo.scene.CommonAttrVO;
import com.iwhalecloud.bss.kite.common.util.KiteBeanUtils;
import com.iwhalecloud.bss.kite.cucc.common.constant.LocalKeyConsts;
import com.iwhalecloud.bss.kite.manager.inst.KiteAttr;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Optional;
import java.util.stream.Collectors;

public class AssemblyAttrDtoUtils {

    /**
     * 构建返回对象
     * @param configs
     * @return
     */
    public static List<CommonAttrVO> assemblyVO(List<InstAttrDTO> configs) {
        if (configs == null) {
            return Collections.emptyList();
        }
        return configs.stream().map(attrDTO -> KiteBeanUtils.transform(attrDTO, CommonAttrVO.class)).collect(Collectors.toList());
    }

    public static List<CommonAttrVO> assemblyCommonVO(List<InstAttrDTO> attrConfigs) {
        List<CommonAttrVO> commonList =  new ArrayList<>();

        attrConfigs.stream().forEach(instAttrDTO -> {
            CommonAttrVO commonAttrVO = new CommonAttrVO();
            commonAttrVO.setValue(Optional.ofNullable(instAttrDTO.getValue()).orElse(""));
            commonAttrVO.setValueDesc(Optional.ofNullable(instAttrDTO.getValueDesc()).orElse(""));
            commonAttrVO.setFieldName(instAttrDTO.getFieldName());
            commonAttrVO.setCname(instAttrDTO.getCname());
            commonAttrVO.setAttrValueTypeId(LocalKeyConsts.ATTRVALUE_TT);
            commonAttrVO.setAttrId(Optional.ofNullable(instAttrDTO.getAttrId()).orElse(""));
            commonList.add(commonAttrVO);
        });

        return commonList;
    }

    public static List<InstAttrDTO> assemblyInstAttr(List<KiteAttr> kiteAttrs) {
        List<InstAttrDTO> accessInstAttr = new ArrayList<>();
        kiteAttrs.stream().forEach(kiteAttr -> {
            InstAttrDTO instAttrDTO = new InstAttrDTO();
            instAttrDTO.setFieldName(kiteAttr.getFieldName());
            instAttrDTO.setCname(kiteAttr.getCname());
            instAttrDTO.setValueDesc(Optional.ofNullable(kiteAttr.getValueDesc()).orElse(kiteAttr.getDefaultValue()));
            instAttrDTO.setValue(Optional.ofNullable(kiteAttr.getValue()).orElse(kiteAttr.getDefaultValue()));
            instAttrDTO.setShowType(kiteAttr.getShowType());
            instAttrDTO.setAttrValueTypeId(LocalKeyConsts.ATTRVALUE_TT);
            accessInstAttr.add(instAttrDTO);
        });
        return accessInstAttr;
    }
}
